package com.lhs.Controller;

import com.lhs.Result.Result;
import com.lhs.VO.blog.BlogVO;
import com.lhs.entity.blog.blog_post;
import com.lhs.service.BlogViewService;
import com.lhs.service.blog_likeService;
import com.lhs.service.blog_post_0Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/blog")
public class BlogViewController {

    @Autowired
    private BlogViewService blogViewService;

    @Autowired
    private blog_post_0Service blogPostService;
    @Autowired
    private blog_likeService blogLikeService;


    /**
     * 获取文章详情（同时增加浏览计数）
     */
    @GetMapping("/{postId}")
    public Result getBlogDetail(@PathVariable Long postId, @RequestHeader(required = false) Long userId) {
        // 增加浏览计数
        blogViewService.incrementViewCount(postId, userId);

        // 获取文章详情
        Result <BlogVO> blogPost = blogPostService.getBlogPostById(postId);

        // 获取点赞信息
        Map<String, Object> likeInfo = blogLikeService.getBlogLikeInfo(postId, userId);

        Map<String, Object> result = new HashMap<>();
        result.put("blog", blogPost);
        result.put("likeInfo", likeInfo);

        return Result.success(result);
    }

    /**
     * 获取文章浏览数
     */
    @GetMapping("/{postId}/view-count")
    public Result getBlogViewCount(@PathVariable Long postId) {
        Long viewCount = blogViewService.getBlogViewCount(postId);
        return Result.success(viewCount);
    }
}